<?php
namespace Admin\Controller;
use Common\Controller\AuthController;
use Think\Controller;
class ServiceController extends AuthController {

    /**
     * service列表
     **/
    public function index(){
        I('val')? $where['service_name']=array('like',"%".I('val')."%"):'';
        $this->assign('val',I('val'));
        A('Public')-> page('service', $where, 10, 'index','');

    }


    /**
     * 添加service
     **/
    public  function add_service(){
        if(IS_POST){
            $m = M('service');
            if (!$m->autoCheckToken($_POST)) {
                $this->error('表单令牌错误', 0, 0);
            }else{
                $data=I();
                $data['add_time']=time();
                $data['service_content'] = html_entity_decode($data['service_content']);
                $data['images'] = base64ToImage($data['images']);
                $data['big_images'] = base64ToImage($data['big_images']);
                $m->add($data)?$this->error('添加成功',U('Service/index')): $this->error('添加失败');
            }
        }else{
            $dance_type=M('service_type')->select();
            $this->assign('service_type',$dance_type);
            $this->display();
        }

    }
    /**
     * 修改service
     **/
    public  function edit_service(){
        if (IS_POST) {
            $data = I();
            $data['add_time'] = time();
            $data['service_content'] = html_entity_decode($data['service_content']);
            $data['images']?$data['images'] = base64ToImage($data['images']):$data['images'] =$data['old_images'] ;
            $data['big_images']?  $data['big_images'] = base64ToImage($data['big_images']):$data['big_images'] =$data['old_big_images'] ;
            M('service')->where(array('id' => I('id')))->setField($data)?$this->success('修改成功',U('Service/index')):$this->error('修改失败');
        } else{
            $data = M('service')->where(array('id' => I('id')))->find();
            $this->assign('service_type',M('service_type')->select());
            $this->assign('data', $data);
            $this->display();
        }
    }
    /**
     * 删除service
     **/
    public function  del_service(){
        I('id')?I('id'):$this->error('此ID不存在');
        M('service')->where(array('id'=>I('id')))->delete()?  $this->success('删除成功'):  $this->error('删除失败');

    }

    /**
     *置顶service
     **/
    public function top_status(){
        I('id')?I('id'):$this->error('ID不存在',U('index'),1);
        $status=M('service')->where(array('id'=>I('id')))->getField('top');//判断当前状态情况
        if($status==1){
            $statedata = array('top'=>0);
            M('service')->where(array('id'=>I('id')))->setField($statedata);
            $this->success('不置顶',1,1);
        }else{
            $statedata = array('top'=>1);
            M('service')->where(array('id'=>I('id')))->setField($statedata);
            $this->success('置顶',1,1);
        }
    }

    /**
     *service类型
     **/
    public function service_type(){
        $data=M('service_type')->select();
        $this->assign('data',$data);
        $this->display();
    }

    /**
     *置顶service_type
     **/
    public function top_type_status(){
        I('id')?I('id'):$this->error('ID不存在',U('index'),1);
        $status=M('service')->where(array('id'=>I('id')))->getField('top');//判断当前状态情况
        if($status==1){
            $statedata = array('top'=>0);
            M('service')->where(array('id'=>I('id')))->setField($statedata);
            $this->success('不置顶',1,1);
        }else{
            $statedata = array('top'=>1);
            M('service')->where(array('id'=>I('id')))->setField($statedata);
            $this->success('置顶',1,1);
        }
    }


    public function del_service_type(){
        I('id')?I('id'):$this->error('此ID不存在');
        M('service_type')->where(array('id'=>I('id')))->delete()?  $this->success('删除成功'):  $this->error('删除失败');
    }


    public function add_service_type(){
        if(IS_POST){
            $data=I();
            $data['add_time']=time();

            M('service_type')->add($data)?$this->success('添加成功'):$this->error('添加失败');
        }else{
            $this->display();
        }
    }

    public function edit_service_type(){
        if(IS_POST){
            $data=array('name'=>I('name'));

            M('service_type')->where(array('id'=>I('id')))->save($data)?$this->success('修改成功'):$this->error('修改失败');

        }else{
            $data=M('service_type')->where(array('id'=>I('id')))->find();
            $this->assign('data',$data);
            $this->display();
        }

    }
}
